
<h1><span class="yiyi-st" id="yiyi-12">numpy.linalg.norm</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.linalg.norm"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.linalg.</code><code class="descname">norm</code><span class="sig-paren">(</span><em>x</em>, <em>ord=None</em>, <em>axis=None</em>, <em>keepdims=False</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/linalg/linalg.py#L1976-L2224"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">矩阵或向量范数。</span></p>
<p><span class="yiyi-st" id="yiyi-15">此函数能够返回八个不同矩阵范数中的一个，或者根据<code class="docutils literal"><span class="pre">ord</span></code>参数的值返回无限数量的矢量范数（如下所述）。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-16">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-17"><strong>x</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-18">输入数组。</span><span class="yiyi-st" id="yiyi-19">如果<em class="xref py py-obj">axis</em>为None，则<em class="xref py py-obj">x</em>必须是1-D或2-D。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-20"><strong>ord</strong>：{非零int，inf，-inf，&apos;fro&apos;，&apos;nuc&apos;}，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-21">规范的顺序（见<code class="docutils literal"><span class="pre">Notes</span></code>下的表格）。</span><span class="yiyi-st" id="yiyi-22">inf表示numpy的<em class="xref py py-obj">inf</em>对象。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-23"><strong>axis</strong>：{int，ints的2元组，无}，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-24">如果<em class="xref py py-obj">axis</em>是整数，则它指定<em class="xref py py-obj">x</em>的轴，沿此轴计算矢量范数。</span><span class="yiyi-st" id="yiyi-25">如果<em class="xref py py-obj">axis</em>是2元组，它指定保存2-D矩阵的轴，并计算这些矩阵的矩阵范数。</span><span class="yiyi-st" id="yiyi-26">如果<em class="xref py py-obj">axis</em>是None，则向量范数（当<em class="xref py py-obj">x</em>是1-D时）或矩阵范数（当<em class="xref py py-obj">x</em>是2-D时）返回。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-27"><strong>keepdims</strong>：bool，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-28">如果设置为True，则规范化的轴将作为尺寸为1的尺寸留在结果中。</span><span class="yiyi-st" id="yiyi-29">使用此选项，结果将相对于原始<em class="xref py py-obj">x</em>正确广播。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-30"><span class="versionmodified">版本1.10.0中的新功能。</span></span></p>
</div>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-31">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-32"><strong>n</strong>：float或ndarray</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-33">矩阵或向量的范数。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<p class="rubric"><span class="yiyi-st" id="yiyi-34">笔记</span></p>
<p><span class="yiyi-st" id="yiyi-35">对于<code class="docutils literal"><span class="pre">ord</span> <span class="pre"> <span class="pre">0</span></span></code>的值，严格来说，结果不是数学“ ，但它仍然可用于各种数值目的。</span></p>
<p><span class="yiyi-st" id="yiyi-36">可以计算以下规范：</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="8%">
<col width="47%">
<col width="44%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-37">ord</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-38">矩阵的范数</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-39">向量的范数</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-40">没有</span></td>
<td><span class="yiyi-st" id="yiyi-41">Frobenius范数</span></td>
<td><span class="yiyi-st" id="yiyi-42">2范数</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-43">&apos;fro&apos;</span></td>
<td><span class="yiyi-st" id="yiyi-44">Frobenius范数</span></td>
<td><span class="yiyi-st" id="yiyi-45"> -   - </span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-46">&apos;nuc&apos;</span></td>
<td><span class="yiyi-st" id="yiyi-47">核准则</span></td>
<td><span class="yiyi-st" id="yiyi-48"> -   - </span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-49">inf</span></td>
<td><span class="yiyi-st" id="yiyi-50">max（sum（abs（x），axis = 1））</span></td>
<td><span class="yiyi-st" id="yiyi-51">max（abs（x））</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-52">-inf</span></td>
<td><span class="yiyi-st" id="yiyi-53">min（sum（abs（x），axis = 1））</span></td>
<td><span class="yiyi-st" id="yiyi-54">min（abs（x））</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-55">0</span></td>
<td><span class="yiyi-st" id="yiyi-56"> -   - </span></td>
<td><span class="yiyi-st" id="yiyi-57">sum（x！= 0）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-58">1</span></td>
<td><span class="yiyi-st" id="yiyi-59">max（sum（abs（x），axis = 0））</span></td>
<td><span class="yiyi-st" id="yiyi-60">如下</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-61">-1</span></td>
<td><span class="yiyi-st" id="yiyi-62">min（sum（abs（x），axis = 0））</span></td>
<td><span class="yiyi-st" id="yiyi-63">如下</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-64">2</span></td>
<td><span class="yiyi-st" id="yiyi-65">2范数（最大唱。</span><span class="yiyi-st" id="yiyi-66">值）</span></td>
<td><span class="yiyi-st" id="yiyi-67">如下</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-68">-2</span></td>
<td><span class="yiyi-st" id="yiyi-69">最小奇异值</span></td>
<td><span class="yiyi-st" id="yiyi-70">如下</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-71">其他</span></td>
<td><span class="yiyi-st" id="yiyi-72"> -   - </span></td>
<td><span class="yiyi-st" id="yiyi-73">sum（abs（x）** ord）**（1.ord）</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-74">Frobenius范数由<a class="reference internal" href="#r41" id="id1">[R41]</a>给出：</span></p>
<blockquote>
<div></div></blockquote>
<p><span class="yiyi-st" id="yiyi-75">核规范是奇异值的总和。</span></p>
<p class="rubric"><span class="yiyi-st" id="yiyi-76">参考文献</span></p>
<table class="docutils citation" frame="void" id="r41" rules="none">
<colgroup><col class="label"><col></colgroup>
<tbody valign="top">
<tr><td class="label"><span class="yiyi-st" id="yiyi-77">[R41]</span></td><td><span class="yiyi-st" id="yiyi-78"><em>（<a class="fn-backref" href="#id1">1</a>，<a class="fn-backref" href="#id2">2</a>）</em> GH Golub and CF Van Loan，<em>Matrix Computations</em>，Baltimore，MD，Johns Hopkins University Press，1985，</span><span class="yiyi-st" id="yiyi-79">15</span></td></tr>
</tbody>
</table>
<p class="rubric"><span class="yiyi-st" id="yiyi-80">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy</span> <span class="k">import</span> <span class="n">linalg</span> <span class="k">as</span> <span class="n">LA</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span> <span class="o">-</span> <span class="mi">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([-4, -3, -2, -1,  0,  1,  2,  3,  4])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([[-4, -3, -2],</span>
<span class="go">       [-1,  0,  1],</span>
<span class="go">       [ 2,  3,  4]])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">7.745966692414834</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">7.745966692414834</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="s1">&apos;fro&apos;</span><span class="p">)</span>
<span class="go">7.745966692414834</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">)</span>
<span class="go">4.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">)</span>
<span class="go">9.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">)</span>
<span class="go">2.0</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">20.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">7.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">-4.6566128774142013e-010</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">6.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">7.745966692414834</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">7.3484692283495345</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
<span class="go">nan</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
<span class="go">1.8570331885190563e-016</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">5.8480354764257312</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="go">nan</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-81">使用<em class="xref py py-obj">轴</em>参数计算向量规范：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
<span class="gp">... </span>              <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="go">array([ 1.41421356,  2.23606798,  5.        ])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">array([ 3.74165739,  4.24264069])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="nb">ord</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">array([ 6.,  6.])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-82">使用<em class="xref py py-obj">轴</em>参数计算矩阵范数：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="go">array([  3.74165739,  11.22497216])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="p">:,</span> <span class="p">:]),</span> <span class="n">LA</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="p">:,</span> <span class="p">:])</span>
<span class="go">(3.7416573867739413, 11.224972160321824)</span>
</pre></div>
</div>
</dd></dl>
